package com.granada;

import com.granada.filter.TokenAuthorFilter;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@SpringBootApplication
@MapperScan({"com.granada.mapper","com.granada.utils","com.granada.service"})
public class WeightApplication {

	public static void main(String[] args) throws UnknownHostException {
//		SpringApplication.run(WeightApplication.class, args);
		ConfigurableApplicationContext application = SpringApplication.run(WeightApplication.class, args);
		Environment env = application.getEnvironment();
		String ip = InetAddress.getLocalHost().getHostAddress();
		String port = env.getProperty("server.port");
		//String path = env.getProperty("server.servlet.context-path");
		String path = "";
		log.info("\n(♥◠‿◠)ﾉﾞ  启动成功   ლ(´ڡ`ლ)ﾞ  \n");
		log.info("\n----------------------------------------------------------\n\t" +
				"Application All-In-One-Card is running! Access URLs:\n\t" +
				"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
				"External: \thttp://" + ip + ":" + port + path + "/\n\t" +
				"Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +
				"----------------------------------------------------------");

	}


//	public static void main(String[] args) throws Exception {
//		//SpringApplication.run(CompanyFrameApplication.class, args);
//		ConfigurableApplicationContext application = SpringApplication.run(WeightApplication.class, args);
//		Environment env = application.getEnvironment();
//		String ip = InetAddress.getLocalHost().getHostAddress();
//		String port = env.getProperty("server.port");
//		//String path = env.getProperty("server.servlet.context-path");
//		String path = "";
//		log.info("\n(♥◠‿◠)ﾉﾞ  启动成功   ლ(´ڡ`ლ)ﾞ  \n");
//		log.info("\n----------------------------------------------------------\n\t" +
//				"Application All-In-One-Card is running! Access URLs:\n\t" +
//				"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
//				"External: \thttp://" + ip + ":" + port + path + "/\n\t" +
//				"Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +
//				"----------------------------------------------------------");
//	}

	//注册filter
	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		TokenAuthorFilter tokenAuthorFilter = new TokenAuthorFilter();
		registrationBean.setFilter(tokenAuthorFilter);
		List<String> urlPatterns = new ArrayList<String>();
		urlPatterns.add("/app/login/register");
		urlPatterns.add("/app/login/applogin");
		urlPatterns.add("/app/login/vaildCode");
		urlPatterns.add("/app/login/phone");
		urlPatterns.add("/api/**");
		urlPatterns.add("/api/login/phone");
		urlPatterns.add("/api/login/code");
		urlPatterns.add("/api/login/decrypt");
		urlPatterns.add("/api/manufactor/login");
		urlPatterns.add("/app/channe/list");
		registrationBean.setUrlPatterns(urlPatterns);
		return registrationBean;
	}
//	@GetMapping("/hello")
//	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
//		return String.format("Hello %s!", name);
//	}

}
